#include "common.h"
#include "graph.h"
#include "message.h"
#include "framework.h"

void processRound(int roundNumber, Node *node)
{
    EdgeList *edges;
    Edge *edge;

    Message message;
    int from = 0;

    if (node != NULL)
    {
        // read and process
        while (recvMessage(node->inbox, &message, &from) == true)
        {
            node->data += message.data[0];
        }

        // prepare what to send
        message.data[0] = node->data;

        edges = node->outwardEdges;
        for (edge = edges->head; edge != NULL; edge = edge->next)
        {
            sendMessage(node->outbox, &message, node->nodeNumber, edge->to);
        }
    }
}

int main()
{
    Node *node;
    Edge *edge;
    Graph *g;

    // initializations
    msgInitialize();
    
    g = createGraph();
    g->nodeComputation = processRound;

    node = createNode();
    node->data = 5;
    node->nodeNumber = 1;
    addNode(g, node);

    node = createNode();
    node->data = 3;
    node->nodeNumber = 2;
    addNode(g, node);
    
    node = createNode();
    node->data = 10;
    node->nodeNumber = 3;
    addNode(g, node);

    edge = createEdge();
    edge->from = 1;
    edge->to = 3;
    addEdge(g, edge);

    edge = createEdge();
    edge->from = 2;
    edge->to = 3;
    addEdge(g, edge);

    startRounds(g);

    printGraph(g);

    DESTROY_GRAPH(g)

    // uninitializations
    msgTerminate();

#ifdef OS_WIN
    getchar();
#endif
    return 0;
}
